package com.ruoyi.bus.service.impl.map;

import com.google.gson.Gson;
import com.ruoyi.bus.domain.WitBus;
import com.ruoyi.bus.domain.WitBusLocation;
import com.ruoyi.bus.domain.WitRoute;
import com.ruoyi.bus.domain.WitSite;
import com.ruoyi.bus.domain.map.*;
import com.ruoyi.bus.springutil.SpringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.yeauty.pojo.Session;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @Description 此类中方法是通过反射调用，维护时请勿更改方法名称
 * @Author caiyanshan
 * @Date 2021/4/28 9:44
 * @Param
 * @Return
 */
@Slf4j
public class MapServiceApi {
  
  /**
   * @Description 首次连接发送消息, 原样返回
   * @Author caiyanshan
   * @Date 2021/4/28 14:09
   * @Param [json, session]
   * @Return java.lang.String
   */
  public String login(String json, Session session) {
    Gson gson = new Gson();
    MapMessage loginMessage = gson.fromJson(json, MapMessage.class);
    Map<String, Object> resultMap = new HashMap<String, Object>();
    if(loginMessage.getType() == null){//没有用户类型
      resultMap.put("code", 500);
      resultMap.put("msg", "请求参数用户类型");
      return gson.toJson(resultMap);
    }
    if(loginMessage.getBusId() == null){//没有用户类型
      resultMap.put("code", 500);
      resultMap.put("msg", "请求参数缺少车辆ID");
      return gson.toJson(resultMap);
    }
    MapSocketSession webSocketSession = new MapSocketSession();
    webSocketSession.setCreateTime(new Date());
    webSocketSession.setSession(session);
    webSocketSession.setBusId(loginMessage.getBusId());
    webSocketSession.setType(loginMessage.getType());
    String token = this.getToken();
    webSocketSession.setToken(token);
    MapSessionCache.setSession(token, webSocketSession);//登录后信息原样返回,可以记录操作日志
    loginMessage.setToken(token);
    return gson.toJson(loginMessage);
  }
  
  /**
   * @Description 心跳, 原样返回
   * @Author caiyanshan
   * @Date 2021/4/28 14:11
   * @Param []
   * @Return java.lang.String
   */
  public String ping(String json) {
    Gson gson = new Gson();
    MapMessage loginMessage = gson.fromJson(json, MapMessage.class);
    Map<String,Object> loginMap = isLogin(loginMessage);
    if(loginMap.size() != 0){
      return gson.toJson(loginMap);
    }
    return json;
  }
  
  /**
   * @Description 上报站点数据
   * @Author caiyanshan
   * @Date 2021/4/28 9:58
   * @Param [json]
   * @Return java.lang.String
   */
  public String uploadLocation(String json) {
    Gson gson = new Gson();
    UploadLocationMessage uploadLocation = gson.fromJson(json, UploadLocationMessage.class);
    Map<String,Object> loginMap = isLogin(uploadLocation);
    if(loginMap.size() != 0){
      return gson.toJson(loginMap);
    }
    WitBusLocation location = new WitBusLocation();
    location.setBusId(uploadLocation.getBusId());
    location.setLatitude(uploadLocation.getLatitude());
    location.setLongitude(uploadLocation.getLongitude());
    location.setRouteId(uploadLocation.getRouteId());
    SpringUtils.getLocationService().insertWitBusLocation(location);//插入到数据库(采用异步的方式)
    Map<String, Object> resultMap = new HashMap<String, Object>();
    resultMap.put("code", 200);
    resultMap.put("msg", "上报成功");
    return gson.toJson(resultMap);
  }
  
  /**
   * @Description 判断是否登录
   * @Author caiyanshan
   * @Date 2021/4/30 10:26
   * @Param []
   * @Return java.lang.String
   */
  private Map<String, Object> isLogin(MapMessage mapMessage) {
    Map<String, Object> resultMap = new HashMap<String, Object>();
    if(StringUtils.isBlank(mapMessage.getToken())){//没有token
      resultMap.put("code", 500);
      resultMap.put("msg", "请求参数缺少token,请登录");
      return resultMap;
    }
    MapSocketSession mapSocketSession = MapSessionCache.getSession(mapMessage.getToken());
    if(mapSocketSession == null){
      resultMap.put("code", 500);
      resultMap.put("msg", "token失效,请登录");
      return resultMap;
    }
    this.refreshLoginTime(mapSocketSession);
    return resultMap;
  }
  
  /**
   * @Description 刷新当前登陆时间
   * @Author caiyanshan
   * @Date 2021/4/30 10:26
   * @Param []
   * @Return void
   */
  private void refreshLoginTime(MapSocketSession mapSocketSession){
    MapSessionCache.setSession(mapSocketSession.getToken(), mapSocketSession);
  }
  
  public String getToken() {
    String token = (System.currentTimeMillis() + new Random().nextInt(999999999)) + "";
    //数据指纹   128位长   16个字节  md5
    try {
      MessageDigest md = MessageDigest.getInstance("md5");
      byte md5[] = md.digest(token.getBytes());
      //base64编码--任意二进制编码明文字符   adfsdfsdfsf
      BASE64Encoder encoder = new BASE64Encoder();
      return encoder.encode(md5);
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(e);
    }
  }
  
  public String loginShowBus(String json) {//暂不存储session,后续如有需要,再存session
    Gson gson = new Gson();
    MapMessage loginMessage = gson.fromJson(json, MapMessage.class);
    return gson.toJson(loginMessage);
  }
  
  public String listRoute(String json){
    Gson gson = new Gson();
    ListLocationMessage ListLocationMessage = gson.fromJson(json, ListLocationMessage.class);
    WitRoute route = new WitRoute();
    route.setRouteId(ListLocationMessage.getRouteId());
    List<WitRoute> witRouteList = SpringUtils.getRouteService().listRouteByNowPlan(route);
    Map<String, Object> resultMap = new HashMap<String, Object>();
    resultMap.put("cmd", "listRoute");
    resultMap.put("data", witRouteList);
    return gson.toJson(resultMap);
  }
  
  /**
   * @Description 废弃方法
   * @Author caiyanshan
   * @Date 2021/6/25 15:31
   * @Param [json]
   * @Return java.lang.String
   */
  public String listBusLocation(String json){
    Gson gson = new Gson();
    ListLocationMessage ListLocationMessage = gson.fromJson(json, ListLocationMessage.class);
    Long routeId = ListLocationMessage.getRouteId();//为空则查所有
    WitBusLocation busLocation = new WitBusLocation();
    busLocation.setRouteId(routeId);
    List<WitBusLocation> witBusLocationList = SpringUtils.getLocationService().listLocationByRouteId(busLocation);
    Map<String, Object> resultMap = new HashMap<String, Object>();
    resultMap.put("cmd", "listBusLocation");
    resultMap.put("data", witBusLocationList);
    return gson.toJson(witBusLocationList);
  }
  
  
  public String getSiteLocation(String json){
    Gson gson = new Gson();
    SiteLocationMessage siteLocationMessage = gson.fromJson(json, SiteLocationMessage.class);
    Long siteId = siteLocationMessage.getSiteId();
    WitSite site = SpringUtils.getWitSiteService().selectWitSiteById(siteId);
    Map<String, Object> resultMap = new HashMap<String, Object>();
    resultMap.put("cmd", "getSiteLocation");
    resultMap.put("data", site);
    return gson.toJson(resultMap);
  }
}